package hlhj.fhp.supreme.fragment

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebViewClient
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.activitys.ClassDetailAty
import hlhj.fhp.supreme.activitys.RedDetailAty
import hlhj.fhp.supreme.adapters.SearchAdp
import hlhj.fhp.supreme.javabean.PhoneBean
import hlhj.fhp.supreme.javabean.SearchBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.toast
import hlhj.fhp.supreme.utils.JsonCallBack
import kotlinx.android.synthetic.main.act_fgm.*
import kotlinx.android.synthetic.main.act_fgm.view.*

/**
 * Created by Administrator on 2018\5\8 0008.
 */
class ActFgm :Fragment(){
    private lateinit var rootView:View
    private lateinit var adp:SearchAdp
    private val searchResults=ArrayList<SearchBean.DataBean>()
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView= inflater.inflate(R.layout.act_fgm, null)

        adp=SearchAdp(searchResults)
        rootView.recyclerview.adapter=adp
        rootView.recyclerview.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)


        initListener()


        return rootView
    }

    private fun initListener() {
        rootView.btSearch.setOnClickListener {
            if (etSearch.text.isEmpty())return@setOnClickListener
            OkGo.post<SearchBean>(Urls.BaseUrl+Urls.SEARCH)
                    .params("keywords",rootView.etSearch.text.toString())
                    .execute(object :JsonCallBack<SearchBean>(activity){
                        override fun onSuccess(response: Response<SearchBean>?) {
                            val searchBean = response?.body()
                            if (searchBean!!.code==1){
                                searchResults.clear()
                                if (searchBean.data==null){

                                }else{
                                    searchResults.addAll(searchBean.data)
                                }
                                adp.notifyDataSetChanged()
                            }else{
                                this.toast(context,searchBean?.msg)
                            }
                        }
                    })
        }
        adp.setOnItemClickListener { adapter, view, position ->
            when(searchResults[position].type){
                1->{
                    val intent = Intent(context, RedDetailAty::class.java)
                    intent.putExtra("id",searchResults[position].id)
                    intent.putExtra("money",searchResults[position].money.toString())
                    context.startActivity(intent)
                }
                2->{
                    val intent = Intent(context, ClassDetailAty::class.java)
                    intent.putExtra("id",searchResults[position].id)
//                    intent.putExtra("lid",searchResults[position]?.level_id.toString())
                    context.startActivity(intent)
                }
            }
        }
    }
}